package com.tc.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tc.dao.TrucksMapper;
import com.tc.pojo.Trucks;
import com.tc.service.TrucksService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/****
 * @Author:admin
 * @Description:Trucks业务层接口实现类
 * @Date 2019/6/14 0:16
 *****/
@Service
public class TrucksServiceImpl implements TrucksService {

    @Autowired
    private TrucksMapper trucksMapper;


    /**
     * Trucks条件+分页查询
     * @param trucks 查询条件
     * @param page 页码
     * @param size 页大小
     * @return 分页结果
     */
    @Override
    public PageInfo<Trucks> findPage(Trucks trucks, int page, int size){
        //分页
        PageHelper.startPage(page,size);
        //搜索条件构建
        Example example = createExample(trucks);
        //执行搜索
        return new PageInfo<Trucks>(trucksMapper.selectByExample(example));
    }

    /**
     * Trucks分页查询
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageInfo<Trucks> findPage(int page, int size){
        //静态分页
        PageHelper.startPage(page,size);
        //分页查询
        return new PageInfo<Trucks>(trucksMapper.selectAll());
    }

    /**
     * Trucks条件查询
     * @param trucks
     * @return
     */
    @Override
    public List<Trucks> findList(Trucks trucks){
        //构建查询条件
        Example example = createExample(trucks);
        //根据构建的条件查询数据
        return trucksMapper.selectByExample(example);
    }


    /**
     * Trucks构建查询对象
     * @param trucks
     * @return
     */
    public Example createExample(Trucks trucks){
        Example example=new Example(Trucks.class);
        Example.Criteria criteria = example.createCriteria();
        if(trucks!=null){
            // 编号
            if(!StringUtils.isEmpty(trucks.getId())){
                    criteria.andEqualTo("id",trucks.getId());
            }
            // 广告表关联编号
            if(!StringUtils.isEmpty(trucks.getPid())){
                    criteria.andEqualTo("pid",trucks.getPid());
            }
            // 货车名称
            if(!StringUtils.isEmpty(trucks.getCdyname())){
                    criteria.andLike("cdyname","%"+trucks.getCdyname()+"%");
            }
            // 货车的小图片
            if(!StringUtils.isEmpty(trucks.getCdyimg())){
                    criteria.andEqualTo("cdyimg",trucks.getCdyimg());
            }
            // 货车长度(厘米)
            if(!StringUtils.isEmpty(trucks.getLength())){
                    criteria.andEqualTo("length",trucks.getLength());
            }
            // 货车宽度(厘米)
            if(!StringUtils.isEmpty(trucks.getBroad())){
                    criteria.andEqualTo("broad",trucks.getBroad());
            }
            // 货车高度(厘米)
            if(!StringUtils.isEmpty(trucks.getHight())){
                    criteria.andEqualTo("hight",trucks.getHight());
            }
            // 载重重量(千克)
            if(!StringUtils.isEmpty(trucks.getLoadcapacity())){
                    criteria.andEqualTo("loadcapacity",trucks.getLoadcapacity());
            }
            // 是否启用 0启用 1禁用
            if(!StringUtils.isEmpty(trucks.getIsdisable())){
                    criteria.andEqualTo("isdisable",trucks.getIsdisable());
            }
            // 载货体积(方)
            if(!StringUtils.isEmpty(trucks.getVolume())){
                    criteria.andEqualTo("volume",trucks.getVolume());
            }
        }
        return example;
    }

    /**
     * 删除
     * @param id
     */
    @Override
    public void delete(Integer id){
        trucksMapper.deleteByPrimaryKey(id);
    }

    /**
     * 修改Trucks
     * @param trucks
     */
    @Override
    public void update(Trucks trucks){
        trucksMapper.updateByPrimaryKey(trucks);
    }

    /**
     * 增加Trucks
     * @param trucks
     */
    @Override
    public void add(Trucks trucks){
        trucksMapper.insert(trucks);
    }

    /**
     * 根据ID查询Trucks
     * @param id
     * @return
     */
    @Override
    public Trucks findById(Integer id){
        return  trucksMapper.selectByPrimaryKey(id);
    }

    /**
     * 查询Trucks全部数据
     * @return
     */
    @Override
    public List<Trucks> findAll() {
        return trucksMapper.selectAll();
    }
}
